package org.budo.support.dao.page;

import java.io.Serializable;

/**
 * 作为分页查询时的参数
 * @author li
 * @version 1 (2016年6月27日 下午8:12:41)
 * @since Java7
 * @see org.budo.support.dao.page.PageModel
 */
public class Page implements Serializable {
    private static final long serialVersionUID = -6026614354807515441L;

    public static final int PAGE_SIZE_DEFAULT = 10;

    private static final int PAGE_NUMBER_DEFAULT = 1;

    private Integer pageNumber = null;

    private Integer pageSize = null;

    private Boolean count = null;

    private Integer recordCount = 0;

    private String orderBy;

    private Integer offset;

    public static final Page max() {
        return new Page(1, Integer.MAX_VALUE);
    }

    public Page() {}

    public Page(Integer pageNumber, Integer pageSize) {
        this.setPageNumber(pageNumber);
        this.setPageSize(pageSize);
    }

    public Page setOffset(Integer offset) {
        this.offset = offset;
        return this;
    }

    public Integer getOffset() {
        return null != offset ? offset : ((null == pageSize ? PAGE_SIZE_DEFAULT : pageSize) * ((null == pageNumber ? PAGE_NUMBER_DEFAULT : pageNumber) - 1));
    }

    public Integer getPageCount() {
        Integer remainder = this.getRecordCount() % this.getPageSize();
        Integer quotient = this.getRecordCount() / this.getPageSize();
        return remainder < 1 ? quotient : quotient + 1;
    }

    public Boolean getCount() {
        return count;
    }

    public Page setCount(Boolean count) {
        this.count = count;
        return this;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public Page setOrderBy(String orderBy) {
        this.orderBy = orderBy;
        return this;
    }

    public Page orderBy(String orderBy) {
        return this.setOrderBy(orderBy);
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Integer getRecordCount() {
        return recordCount;
    }

    public Page setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
        return this;
    }

    public Page setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public Page setRecordCount(Number recordCount) {
        this.recordCount = recordCount.intValue();
        return this;
    }

    public String toString() {
        return super.toString() //
                + ", pageNumber=" + this.getPageNumber() //
                + ", pageSize=" + this.getPageSize() //
                + ", recordCount=" + this.getRecordCount();
    }
}
